/* 对申请列表进行增删改查的操作 */
// 链接数据库
const db = uniCloud.database();
// 获取 `apply` 集合的引用
const collection = db.collection('Apply_List');

// 获取用户申请信息列表
const getApplyList = async (data) => {
	try {
		const res = await collection.get()
		return res
	} catch (e) {
		//TODO handle the exception
	}
}
// 新增用户申请信息列表
const setApplyList = async (data) => {
	try {
		const res = await collection.add(data)
		return res
	} catch (e) {
		//TODO handle the exception
	}
}
// 修改用户申请信息列表
const updateApplyList = async (key, data) => {
	try {
		const res = await collection.where({userId: key}).update({data});
		return res
	} catch (e) {
		//TODO handle the exception
	}
}
// 查找申请人
const findApplyList = async (data) => {
	try {
		const res = await collection.where(data).get()
		return res
	} catch (e) {
		//TODO handle the exception
	}
}


/* 
 * event为客户端上传的参数
 */
exports.main = async (event, context) => {
	console.log('event', event);
	const {action,params, key} = event || {}
	switch (action) {
		case 'getApplyList':
			return await getApplyList(params);
			break;
		case 'setApplyList':
			return await setApplyList(params);
			break;
		case 'updateApplyList':
			return await updateApplyList(key,params);
			break;
		case 'findApplyList':
			return await findApplyList(params);
			break;
		default:
			return await getApplyList(params);
			break;
	}
};
